# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.

import time
import pyautogui
import pyperclip
import os
import random
import requests


# TODO hot search head
head = {
    'Host': 'weibo.com',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/91.0.4472.114 Safari/537.36',
    'Cookie': '_s_tentry=passport.weibo.com; Apache=6356442860734.135.1625190461834; '
              'SINAGLOBAL=6356442860734.135.1625190461834; ULV=1625190461839:1:1:1:6356442860734.135.1625190461834:; '
              'login_sid_t=c93ac5171576f17a909eed12c5e8cfc4; cross_origin_proto=SSL; '
              'WBtopGlobal_register_version=2021070513; XSRF-TOKEN=8vjbFspGr07Sn-cdcGF9bE8p; '
              'SCF=AlW7rSh1JgT8onS83-vG499mrb6Dvqd_WUYAR_9wLnK7V8u2xSPSntcLT6FCXYRmv6zSRY_ZwN6SOu6gucMKTdo.; '
              'WBPSESS=A'
              '-1gawSeNXq9paSfMN9OEE4h5PqVat4csQaXm1vz6u1vfzouxYnXyFU5Svie2nM0YZAeTXJyNH2SG5egKII1yAH717WTY4XaAaE7Yw0brC817xbcnPqFr2yilPRp6PHg; wb_view_log=1920*10801; UOR=,,login.sina.com.cn; YF-V-WEIBO-G0=b09171a17b2b5a470c42e2f713edace0; SUB=_2A25N6X37DeRhGeFM6lcT-CbFzD-IHXVun-gzrDV8PUNbmtB-LWTfkW9NQPcjlRvkJ_sYgu-OgbaVmBSnynVbNPzH; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhcS_baEAOGyZh0EgaCgz-J5JpX5KzhUgL.FoMEeK-E1hn4S0e2dJLoI7DhqPi.Ig8oPXST; ALF=1657684267; SSOLoginState=1626148267 '
}


# TODO Crawler weibo hot search
def spider_hot_search():
    hs_url = 'https://weibo.com/ajax/side/hotSearch'
    # TODO get json file
    resp = requests.get(hs_url, headers=head, verify=False).json()
    # print(resp)
    datas = resp.get('data').get('realtime')
    hot_search = []
    for data in datas:
        hot = data.get('word')
        hot_search.append(hot)
    print(hot_search)
    # TODO write hot search into txt
    f = open("doc/hotSearch", 'w')
    for line in hot_search:
        f.write(line + "\n")
    f.close()


def send_hot_search():
    # TODO open file
    hot_file = open('doc/hotSearch', "r", encoding="UTF-8")
    hot_content = hot_file.readlines()
    print(len(hot_content))
    for line in range(0, len(hot_content)):
        print("line is {}".format(line))
        # TODO copy the line to copy clip
        pyperclip.copy(hot_content[line])
        print(hot_content[line])
        # TODO paste info
        pyautogui.keyDown('command')
        pyautogui.keyDown('v')
        pyautogui.keyUp('v')
        pyautogui.keyUp('command')
        pyautogui.press('enter')
    # TODO close file
    hot_file.close()


def mapping_click(pos, click):
    # TODO move mouse to target
    pyautogui.moveTo(pos)
    # Judge double or single click
    if click == 'double':
        pyautogui.doubleClick()
    else:
        pyautogui.click()
    time.sleep(1)


def chat_user(user):
    search_pos = (466, 231)
    pyautogui.moveTo(search_pos)
    # TODO click to search
    pyautogui.click(search_pos)
    # TODO type user to search
    pyautogui.typewrite(user)
    # wait for result
    time.sleep(1)
    # TODO get user
    pyautogui.moveRel(xOffset=0, yOffset=80)
    pyautogui.click()
    time.sleep(1)


def read_text(txt):
    # TODO open file
    hello_file = open(txt, "r", encoding="UTF-8")
    file_content = hello_file.readlines()
    print(len(file_content))
    number = random.randint(0, len(file_content) - 1)
    print("line number is {}".format(number))
    # TODO copy the line to copy clip
    pyperclip.copy(file_content[number])
    print(file_content[number])
    # TODO paste info
    pyautogui.keyDown('command')
    pyautogui.keyDown('v')
    pyautogui.keyUp('v')
    pyautogui.keyUp('command')
    # TODO close file
    hello_file.close()


def read_img(img_name):
    pass


def main():
    # Use a breakpoint in the code line below to debug your script.
    os.chdir('/Users/han.yang/Desktop/autoWeChat')
    print(os.getcwd())
    # TODO get hot search file
    spider_hot_search()
    # TODO login weChat
    wechat_pos = (1603, 174)
    mapping_click(wechat_pos, "double")
    login_pos = (898, 665)
    mapping_click(login_pos, "single")
    # TODO Search chat user
    chat_user('QSmally')
    # TODO Start chatting for hello
    read_text('doc/hello')
    # TODO Send hello Message
    pyautogui.press('enter')
    time.sleep(1)
    # TODO Chatting love
    read_text('doc/love')
    # TODO Send info Message
    pyautogui.press('enter')
    time.sleep(1)
    # TODO Send hot search
    send_hot_search()


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    main()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
